package com.VolunteerPlatform.SecurityService;

import com.VolunteerPlatform.Entity.AdminInfo;
import com.VolunteerPlatform.Entity.AdminRoleInfo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * Created by DoveXL on 2018/5/30.
 */
public class UserRoleDetails extends AdminInfo implements UserDetails {

    private List<AdminRoleInfo> userRoles;


    public UserRoleDetails(AdminInfo userLogin,List<AdminRoleInfo> userRoles)
    {
        super(userLogin);
        this.userRoles = userRoles;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(userRoles == null || userRoles.size() <1){
            return AuthorityUtils.commaSeparatedStringToAuthorityList("");
        }
        StringBuilder commaBuilder = new StringBuilder();
        for(AdminRoleInfo role : userRoles){
            commaBuilder.append(role.getAdminID()).append(",");
        }
        String authorities = commaBuilder.substring(0,commaBuilder.length()-1);
        return AuthorityUtils.commaSeparatedStringToAuthorityList(authorities);
    }


    public List<AdminRoleInfo> getUserRoles()
    {
        return this.userRoles;
    }
    @Override
    public String getUsername()
    {
        return  super.getUserName();
    }

    @Override
    public String getPassword()
    {
        return super.getPassword();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        if (super.getUserStatus().equals("NotForbidden"))
        {
            return true;
        }else if (super.getUserStatus().equals("Forbidden"))
        {
            return false;
        }else
        {
            return false;
        }
    }






}
